package development;

import iplayparticle.view.iplayparticleResources;
import state.GameConstants;
import state.GameForm;
import state.GameImage;
import state.Graphics;
import state.TimerListener;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import development.socket.SocketClientUtil;

public class FrmAcceptInviteGame extends GameForm implements TimerListener {
  private GameImage loadingImage1;
  private GameImage loadingImage2;

  private ImageText text8;

  public String message;
  public String message1;

  private int count;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmAcceptInviteGame();
    }
    return singleton;
  }

  public void init(Hashtable hashtable) {
    SocketClientUtil.acceptInviteUserToTable(Global.gameActionHandle.gameBussiness.invitedTableId);
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    loadingImage1 = new GameImage(iplayparticleResources.image_loading_1, null, null);
    loadingImage2 = new GameImage(iplayparticleResources.image_loading_2, null, null);

    setTimerListener(this);
    message = "Đang kết nối, xin vui lòng chờ";
    message1 = "";
  }
  
  private void initImage() {
  }

  public void draw(Graphics g) {
    initImage();
    
    DrawUtils.drawBackground(g);
    
    int x1 = 0;
    int y1 = 0;
    if (GameConstants.IS_240x320_SCREEN) {
      text8.drawString2(g, message, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 210, GameConstants.TOP_HCENTER_ANCHOR);
      text8.drawString2(g, message1, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 264, GameConstants.TOP_HCENTER_ANCHOR);
      x1 = GameConstants.SCREEN_WIDTH / 2 - 58;
      y1 = 247;
    } else {
      text8.drawString2(g, message, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 140, GameConstants.TOP_HCENTER_ANCHOR);
      text8.drawString2(g, message1, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 193, GameConstants.TOP_HCENTER_ANCHOR);
      x1 = GameConstants.SCREEN_WIDTH / 2 - 58;
      y1 = 172;
    }

    int index = count % 11;
    for (int i = 1; i < 11; i++) {
      if (i <= index) {
        g.drawImage(loadingImage1, x1 + (loadingImage1.getWidth() + 1) * (i - 1), y1, GameConstants.TOP_LEFT_ANCHOR);
      } else {
        g.drawImage(loadingImage2, x1 + (loadingImage1.getWidth() + 1) * (i - 1), y1, GameConstants.TOP_LEFT_ANCHOR);
      }
    }
  }

  public void doTask() {
    count++;
  }

  public void destroy() {
    loadingImage1 = null;
    loadingImage2 = null;
    text8 = null;
  }
}
